<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像区域-->
      <div class="avator_box">
        <img src="./../assets/head.jpg" alt="" />
      </div>

      <!--表单区域-->
      <el-form
        :model="loginform"
        ref="loginFormref"
        class="login_form"
        :rules="loginFormRules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginform.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder=""
            v-model="loginform.password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="Login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginform: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        // 验证登录名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      // alert(this.$refs.loginFormref)
      this.$refs.loginFormref.resetFields()
    },
    Login() {
      
      this.$refs.loginFormref.validate(async valid => {
        if (!valid) {
          return
        }
        // console.log(async)
        const { data: res } = await this.$axios
          .post('login', this.loginform)
          .catch(function() {
            this.$message.error('登录失败')

          })
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        // console.log(res)
        // 1.将登录之后的 token 保存到客户端 sesionStorge 中
        //  1.1 项目中除了登陆之外的其他  api 接口必须在登录之后才可以访问
        //  1.2 token 只应在当前网站打开期间生效，
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="less" scope>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  background-color: #fff;
  width: 450px;
  height: 300px;
  border-radius: 3px;
  left: 50%;
  top: 50%;
  position: absolute;
  transform: translateX(-50%) translateY(-50%);
}
.avator_box {
  width: 130px;
  height: 130px;
  padding: 10px;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  background-color: #fff;

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: lightgray;
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0px;
  width: 80%;
  padding: 0% 10%;
}
</style>
